package com.inst.jsoup_html_demo.fragment;

import android.os.Bundle;
import android.support.annotation.IdRes;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.support.v4.view.ViewPager;
import android.support.v7.widget.Toolbar;
import android.util.DisplayMetrics;
import android.util.TypedValue;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.animation.Animation;
import android.view.animation.AnimationUtils;
import android.view.animation.LayoutAnimationController;
import android.widget.ImageView;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.inst.jsoup_html_demo.R;
import com.inst.jsoup_html_demo.adapter.ViewpagerAdapter;
import com.inst.jsoup_html_demo.utils.DataGenerator;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.Unbinder;


/**
 *
 */

public class TopicFragment extends Fragment {


    @BindView(R.id.iv_search)
    ImageView mIvSearch;
    @BindView(R.id.radio_button_hauti_home)
    RadioButton mRadioButtonHautiHome;
    @BindView(R.id.radio_button_hauti_zuixin)
    RadioButton mRadioButtonHautiZuixin;
    @BindView(R.id.radio_button_hauti_remen)
    RadioButton mRadioButtonHautiRemen;
    @BindView(R.id.radio_button_hauti_jinghua)
    RadioButton mRadioButtonHautiJinghua;
    @BindView(R.id.radio_group_button_caipu)
    RadioGroup mRadioGroupButtonCaipu;
    @BindView(R.id.iv_add)
    ImageView mIvAdd;
    @BindView(R.id.tv_facaipu)
    TextView mTvFacaipu;
    @BindView(R.id.rl_facaifu)
    RelativeLayout mRlFacaifu;
    @BindView(R.id.toolbar)
    Toolbar mToolbar;
    @BindView(R.id.view_pager)
    ViewPager mViewPager;
    private String mFrom;
    Unbinder unbinder;
    private ViewpagerAdapter mAdapter;
    private List<View> views = new ArrayList<>();
    private RadioGroup mRadioGroup;
    private ArrayList<Fragment> mFragments;
    private LayoutAnimationController mAnimationController;

    public static TopicFragment newInstance(String from) {
        TopicFragment fragment = new TopicFragment();
        Bundle bundle = new Bundle();
        bundle.putString("from", from);
        fragment.setArguments(bundle);
        return fragment;
    }

    @Override
    public void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        if (getArguments() != null) {
            mFrom = getArguments().getString("from");
        }

    }

    View view;
    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        view = inflater.inflate(R.layout.topic_fragment_layout, null);
        unbinder = ButterKnife.bind(this, view);
        mFragments = DataGenerator.getHuaTiFragments("RadioGroup___ Tab");
        initView();
        return view;
    }

    float value;

    private void initView() {
        //初始化动画
        Animation animation = AnimationUtils.loadAnimation(getActivity(), R.anim.text_alpha);
        mAnimationController = new LayoutAnimationController(animation);
        mAnimationController.setOrder(LayoutAnimationController.ORDER_NORMAL);

        DisplayMetrics dm = getResources().getDisplayMetrics();
        value = dm.scaledDensity;


        mAdapter = new ViewpagerAdapter(this.getFragmentManager(), mFragments);
        mViewPager.setAdapter(mAdapter);
        //初始化标题栏字体大小
        chageFont(0);
        mViewPager.setCurrentItem(0);
        mRadioGroupButtonCaipu.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {

            @Override
            public void onCheckedChanged(RadioGroup group, @IdRes int checkedId) {
                switch (checkedId) {
                    case R.id.radio_button_hauti_home:
                        chageFont(0);
                        mViewPager.setCurrentItem(0, true);// 去掉切换页面的动画
                        break;
                    case R.id.radio_button_hauti_zuixin:
                        chageFont(1);
                        mViewPager.setCurrentItem(1, true);// 去掉切换页面的动画
                        break;
                    case R.id.radio_button_hauti_remen:
                        chageFont(2);
                        mViewPager.setCurrentItem(2, true);// 去掉切换页面的动画
                        break;
                    case R.id.radio_button_hauti_jinghua:
                        chageFont(3);
                        mViewPager.setCurrentItem(3, true);// 去掉切换页面的动画
                        break;
                }
            }
        });

        mViewPager.addOnPageChangeListener(new ViewPager.OnPageChangeListener() {
            @Override
            public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {

            }

            @Override
            public void onPageSelected(int position) {
                mFragments.get(position);// 获取当前被选中的页面, 初始化该页面数据
                chageFont(position);
            }

            @Override
            public void onPageScrollStateChanged(int state) {

            }
        });
    }

    @Override
    public void onDestroyView() {
        super.onDestroyView();
        unbinder.unbind();
    }

    /***
     *  字体切换
     */
    public void chageFont(int position) {
        if (position == 0) {
            mRadioButtonHautiHome.setTextSize(TypedValue.COMPLEX_UNIT_SP, getResources().getDimension(R.dimen.dimens_8));
            mRadioButtonHautiZuixin.setTextSize(TypedValue.COMPLEX_UNIT_SP, getResources().getDimension(R.dimen.dimens_9));
            mRadioButtonHautiRemen.setTextSize(TypedValue.COMPLEX_UNIT_SP, getResources().getDimension(R.dimen.dimens_9));
            mRadioButtonHautiJinghua.setTextSize(TypedValue.COMPLEX_UNIT_SP, getResources().getDimension(R.dimen.dimens_9));
        } else if (position == 1) {
            mRadioButtonHautiHome.setTextSize(TypedValue.COMPLEX_UNIT_SP, getResources().getDimension(R.dimen.dimens_9));
            mRadioButtonHautiZuixin.setTextSize(TypedValue.COMPLEX_UNIT_SP, getResources().getDimension(R.dimen.dimens_8));
            mRadioButtonHautiRemen.setTextSize(TypedValue.COMPLEX_UNIT_SP, getResources().getDimension(R.dimen.dimens_9));
            mRadioButtonHautiJinghua.setTextSize(TypedValue.COMPLEX_UNIT_SP, getResources().getDimension(R.dimen.dimens_9));
        } else if (position == 2) {
            mRadioButtonHautiHome.setTextSize(TypedValue.COMPLEX_UNIT_SP, getResources().getDimension(R.dimen.dimens_9));
            mRadioButtonHautiZuixin.setTextSize(TypedValue.COMPLEX_UNIT_SP, getResources().getDimension(R.dimen.dimens_9));
            mRadioButtonHautiRemen.setTextSize(TypedValue.COMPLEX_UNIT_SP, getResources().getDimension(R.dimen.dimens_8));
            mRadioButtonHautiJinghua.setTextSize(TypedValue.COMPLEX_UNIT_SP, getResources().getDimension(R.dimen.dimens_9));
        } else if (position == 3) {
            mRadioButtonHautiHome.setTextSize(TypedValue.COMPLEX_UNIT_SP, getResources().getDimension(R.dimen.dimens_9));
            mRadioButtonHautiZuixin.setTextSize(TypedValue.COMPLEX_UNIT_SP, getResources().getDimension(R.dimen.dimens_9));
            mRadioButtonHautiRemen.setTextSize(TypedValue.COMPLEX_UNIT_SP, getResources().getDimension(R.dimen.dimens_9));
            mRadioButtonHautiJinghua.setTextSize(TypedValue.COMPLEX_UNIT_SP, getResources().getDimension(R.dimen.dimens_8));
        }
    }
}
